#include <iostream>
#include <string>

// 抽象原型类
class Dog {
    public:
    virtual ~Dog(){};
    virtual Dog* clone() = 0;  // 克隆方法
    virtual void play() = 0;   // 其他公共接口
};

// 具体原型类
class Twoha : public Dog {
    public:
    Twoha(std::string name):name_(name) {}
    // 拷贝构造函数（深拷贝）
    Twoha(const Twoha& lhs) {
        // 存在指针则必须使用深拷贝
        name_ = lhs.name_;
    }
    // 实现抽象方法
    virtual Dog* clone() {
        return new Twoha(*this);
    }
    virtual void play() {
        std::cout << "我是一只" << name_ << std::endl;
    }
    private:
    std::string name_;
};
int main() {
    std::cout << "hello prototype!!!" << std::endl;
    Dog* dog = new Twoha("二哈");
    Dog* Eha1 = dog->clone();
    Dog* Eha2 = dog->clone();
    Eha1->play();
    Eha2->play();

    delete dog;
    delete Eha1;
    delete Eha2;
    dog = nullptr;
    Eha1 = nullptr;
    Eha2 = nullptr;
    return 0;
}